﻿using System;
using System.Collections.Generic;
using System.Text.Json;
using Just.Library.MemoryQueue;
using Just.Library.Util;

namespace ConsoleAppFramework.Util
{
    public class QueueTestModel
    { 
       public string Name { get; set; }
    }

    public class QueueManagerHelper
    {
        private static readonly string OperaterQueueName = "QueueTest";

        static QueueManagerHelper()
        {
            QueueManager.CreateQueue<QueueTestModel>(OperaterQueueName, ConsumeOperateQueue);
            LogHelper.WriteInfo("LogQueueProvider创建内存队列成功");
        }
        private static AsyncQueue<QueueTestModel> OperateQueue
        {
            get { return QueueManager.GetQueue<QueueTestModel>(OperaterQueueName); }
        }
        private static void ConsumeOperateQueue(List<QueueTestModel> lstInfo)
        {
            try
            {
                ConsoleHelper.WriteSuccessLine($"Get Data{JsonSerializer.Serialize(lstInfo)}");
            }
            catch (Exception ex)
            {
                LogHelper.WriteException("ConsumeOperateQueue:" + lstInfo.Count, ex);
            }
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="queueTestModel"></param>
        public static void AddQueueTestModel(QueueTestModel queueTestModel)
        {

            bool isQueue = true;
            try
            {
                string ip = GetClientIp();
                if (isQueue)
                {
                    ConsoleHelper.WriteWarningLine(ip);
                    OperateQueue.PutMessage(queueTestModel);
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                //var feishudto = new FeiShuMsgDto
                //{
                //    Msg = $"OperateLog报错:{ex.Message}",
                //    Group = FeiShuGroupEnum.业务报警,
                //};
                //RmqQueueManager.Enqueue(feishudto, RmqQueueManager.QueueName.线上飞书消息);
            }
        }

        public static string GetClientIp()
        {

            if (System.Web.HttpContext.Current == null)
            {
                return string.Empty;
            }

            string ip = System.Web.HttpContext.Current.Request.Headers["X-Forwarded-For"];

            if (string.IsNullOrEmpty(ip))
            {
                ip = System.Web.HttpContext.Current.Request.UserHostAddress;
            }

            return ip;
        }
    }
}
